﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Temp;

namespace YArchitech.HVAC
{
	public partial class MainForm : System.Windows.Forms.Form
	{
		public BuildingSystem Building
		{
			get
			{
				return this.buildingSystem;
			}
		}

		public MainForm()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
		}

		public MainForm(BuildingSystem building, HYHeatingSystemOption option) : this()
		{
			this.buildingSystem = building;
			this.heatingOption = option;
			this.heatingCalc = new HeatingSystemCalc();
			this.heatingCalc.SetPipeTemp(this.buildingSystem, this.heatingOption);
		}

		public MainForm(UIDocument UIdoc) : this()
		{
			this.uiDoc = UIdoc;
			this.doc = this.uiDoc.Document;
			this.heatingOption = new HYHeatingSystemOption();
			this.heatingOption.Load(Product.UserConfigFile);
			this.pick = new PickUpSystem(this.uiDoc);
			this.buildingSystem = this.pick.PickUp(this.heatingOption);
			this.heatingCalc = new HeatingSystemCalc();
			this.pick.SetPipeTemp(this.heatingOption);
			this.hasSystem = (this.buildingSystem != null);
			this.InitializeTreeView();
			this.InitializeDataGrid();
			string msg = (!this.hasSystem) ? "请选取采暖干管提取系统：" : "数据准备就绪！";
			this.ShowStatusMessage(msg, 2);
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
		}

		private void InitializeTreeView()
		{
			if (this.buildingSystem == null)
			{
				return;
			}
			this.tvPipes.Nodes.Add(this.buildingSystem.ToTreeNode());
		}

		private void InitializeDataGrid()
		{
			this.InitComoBox();
			this.gvDataShow.Controls.Add(this.cmbPipeDN);
			this.ShowStatusMessage("数据信息加载完成！", 1);
		}

		private void InitComoBox()
		{
			this.cmbPipeDN.Name = "PipeDN";
			this.cmbPipeDN.SelectedIndexChanged += this.defcmb_SelectedIndexChanged;
			int[] dataSource = new int[]
			{
				8,
				15,
				20,
				25,
				32,
				40,
				50,
				60,
				80,
				100,
				125,
				150
			};
			this.cmbPipeDN.DataSource = dataSource;
			this.cmbPipeDN.Visible = false;
		}

		private void TSMIParasSetting_Click(object sender, EventArgs e)
		{
			OptionForm optionForm = new OptionForm(this.heatingOption);
			if (DialogResult.OK == optionForm.ShowDialog())
			{
				this.heatingOption = optionForm.m_Option;
				this.pick.SetPipeTemp(this.heatingOption);
			}
		}

		private void TSMIManage_Click(object sender, EventArgs e)
		{
		}

		private void TSMIPickUp_Click(object sender, EventArgs e)
		{
			string msg = "提取系统数据完成！";
			base.DialogResult = DialogResult.Retry;
			base.Close();
			this.ShowStatusMessage(msg, 1);
		}

		private void TSMIModify_Click(object sender, EventArgs e)
		{
			string msg = "数据修改完成！";
			this.ShowStatusMessage(msg, 0);
		}

		private void TSMIDesign_Click(object sender, EventArgs e)
		{
			if (!this.hasSystem)
			{
				this.ShowStatusMessage("请先提取系统！", 0);
				return;
			}
			this.heatingCalc.DesignCalc(this.buildingSystem, this.heatingOption);
			this.UpdataData(this.tvPipes.SelectedNode);
			this.ShowStatusMessage(this.buildingSystem.WorstCircleInfo, 1);
			this.bCalced = true;
		}

		private void TSMICheck_Click(object sender, EventArgs e)
		{
			if (!this.bCalced)
			{
				this.ShowStatusMessage("请先进行设计计算！！", 0);
				return;
			}
			this.heatingCalc.CheckedCalc(this.buildingSystem, this.heatingOption);
			this.UpdataData(this.tvPipes.SelectedNode);
			this.ShowStatusMessage(this.buildingSystem.WorstCircleInfo, 1);
		}

		private void TSMIExcel_Click(object sender, EventArgs e)
		{
			if (!this.bCalced)
			{
				this.ShowStatusMessage("请先进行设计计算！！", 0);
				return;
			}
			ExcelProgessBar excelProgessBar = new ExcelProgessBar(this.buildingSystem, this.heatingOption);
			excelProgessBar.ShowDialog();
			if (excelProgessBar.Succeed)
			{
				this.ShowStatusMessage("导出计算书完成！", 1);
				return;
			}
			this.ShowStatusMessage("导出计算书出错！", 0);
		}

		private void TSMIReback_Click(object sender, EventArgs e)
		{
			SavePipe.FilePath = this.heatingOption.DataFilePath;
			if (!this.bCalced)
			{
				this.ShowStatusMessage("请先进行设计计算！！", 0);
				return;
			}
			Transaction transaction = new Transaction(this.doc, "赋回Revit");
			transaction.Start();
			try
			{
				this.buildingSystem.BackRevit();
				this.doc.Regenerate();
				transaction.Commit();
				this.ShowStatusMessage("赋回完成！", 1);
			}
			catch (Exception ex)
			{
				Log.Info(new string[]
				{
					ex.Message + ex.StackTrace
				});
				transaction.RollBack();
			}
		}

		private void TSMIBranchLong_Click(object sender, EventArgs e)
		{
		}

		private void TSMIBranchWorst_Click(object sender, EventArgs e)
		{
		}

		private void TSMIBranchMin_Click(object sender, EventArgs e)
		{
		}

		private void TSMIBranchAll_Click(object sender, EventArgs e)
		{
		}

		private void TSMIHyhelp_Click(object sender, EventArgs e)
		{
			string text = HYDataAccess.hyGetHelpChm();
			string text2 = HYDataAccess.hyGetHelpIndex("水系统计算");
			if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
			{
				Help.ShowHelp(new System.Windows.Forms.Control(), text, text2);
			}
		}

		private void TSMICalcHelp_Click(object sender, EventArgs e)
		{
			new IntroSystemCal().ShowDialog();
		}

		private void ShowStatusMessage(string msg, int nPass)
		{
			this.toolStripStatusLabel1.Text = msg;
			switch (nPass)
			{
			case 0:
				this.toolStripStatusLabel1.Image = this._imgFaile;
				return;
			case 1:
				this.toolStripStatusLabel1.Image = this._imgPass;
				return;
			case 2:
				this.toolStripStatusLabel1.Image = this._imgWait;
				return;
			default:
				return;
			}
		}

		private void dgvPipesInfo_Scroll(object sender, ScrollEventArgs e)
		{
		}

		private void gvDataShow_Scroll(object sender, ScrollEventArgs e)
		{
			this.cmbPipeDN.Visible = false;
		}

		private void gvDataShow_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (this.gvDataShow.ColumnCount < 5 || e.RowIndex == -1)
			{
				this.cmbPipeDN.Visible = false;
				return;
			}
			int columnIndex = e.ColumnIndex;
			int rowIndex = e.RowIndex;
			if (columnIndex == 2)
			{
                System.Drawing.Rectangle cellDisplayRectangle = this.gvDataShow.GetCellDisplayRectangle(columnIndex, rowIndex, false);
				string selectedItem = this.gvDataShow.CurrentCell.Value.ToString();
				this.cmbPipeDN.SelectedItem = selectedItem;
				this.cmbPipeDN.Left = cellDisplayRectangle.Left;
				this.cmbPipeDN.Top = cellDisplayRectangle.Top;
				this.cmbPipeDN.Width = cellDisplayRectangle.Width;
				this.cmbPipeDN.Height = cellDisplayRectangle.Height;
				this.cmbPipeDN.Visible = true;
				return;
			}
			if (columnIndex != 4)
			{
				this.cmbPipeDN.Visible = false;
				return;
			}
			this.cmbPipeDN.Visible = false;
			if (!this.bCalced)
			{
				this.ShowStatusMessage("请先进行设计计算！", 0);
				return;
			}
			ResCoeLocalForm resCoeLocalForm = new ResCoeLocalForm((this.gvDataShow.Rows[rowIndex].Tag as HyCalcPipe).Valves);
			resCoeLocalForm.ShowDialog();
			if (resCoeLocalForm.DialogResult == DialogResult.OK)
			{
				this.TSMICheck_Click(null, null);
			}
			this.ShowStatusMessage("局阻系数设定完成！", 1);
		}

		private void dgvPipesInfo_CellClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void defcmb_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.gvDataShow.CurrentCell.Value = this.cmbPipeDN.Text;
			int dn = int.Parse(this.cmbPipeDN.Text);
			int rowIndex = this.gvDataShow.CurrentCell.RowIndex;
			(this.gvDataShow.Rows[rowIndex].Tag as HyCalcPipe).DN = dn;
		}

		private void InitGridView(int categroyNum)
		{
			this.gvDataShow.Rows.Clear();
			this.gvDataShow.Columns.Clear();
			switch (categroyNum)
			{
			case 0:
				this.gvDataShow.Columns.Add(this.CreatColumn("管段编号", true, 40, true));
				this.gvDataShow.Columns.Add(this.CreatColumn("流量(m3/h)", true, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("公称直径", false, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("长度(m)", true, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("∑ξ", false, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("V(m/s)", true, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("Rm(Pa/m)", true, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("ΔPy(Pa)", true, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("ΔPj(Pa)", true, 40, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("ΔP(Pa)", true, 40, false));
				return;
			case 1:
				this.gvDataShow.Columns.Add(this.CreatColumn("散热器编号", true, 60, true));
				this.gvDataShow.Columns.Add(this.CreatColumn("类型", true, 80, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("温控阀压降(Pa)", true, 60, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("不平衡率(%)", true, 60, false));
				return;
			case 2:
				this.gvDataShow.Columns.Add(this.CreatColumn("管段数", true, 50, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("不平衡率(%)", true, 50, false));
				return;
			case 3:
				this.gvDataShow.Columns.Add(this.CreatColumn("环路数", true, 50, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("不平衡率(%)", true, 60, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("控制阀资用压力", true, 80, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("控制阀控制压差", true, 80, false));
				return;
			case 4:
				this.gvDataShow.Columns.Add(this.CreatColumn("户内系统数", true, 50, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("不平衡率(%)", true, 50, false));
				return;
			case 5:
				this.gvDataShow.Columns.Add(this.CreatColumn("楼层数", true, 50, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("不平衡率(%)", true, 50, false));
				return;
			case 6:
				this.gvDataShow.Columns.Add(this.CreatColumn("公用立管数", true, 50, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("总流量(m3/h)", true, 50, false));
				this.gvDataShow.Columns.Add(this.CreatColumn("总压降(Pa)", true, 50, false));
				return;
			default:
				return;
			}
		}

		private DataGridViewTextBoxColumn CreatColumn(string header, bool readOnly, int minWidth, bool bFrozen = false)
		{
			return new DataGridViewTextBoxColumn
			{
				AutoSizeMode = (bFrozen ? DataGridViewAutoSizeColumnMode.None : DataGridViewAutoSizeColumnMode.Fill),
				Frozen = bFrozen,
				MinimumWidth = minWidth,
				ReadOnly = readOnly,
				HeaderText = header
			};
		}

		private void tvPipes_AfterSelect(object sender, TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			if (node == null)
			{
				return;
			}
			this.UpdataData(node);
		}

		private void UpdataData(TreeNode tnSelected)
		{
			string name = tnSelected.Tag.GetType().Name;
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(name);
			if (num <= 1581676155u)
			{
				if (num <= 550359471u)
				{
					if (num != 275222156u)
					{
						if (num != 550359471u)
						{
							return;
						}
						if (!(name == "PipeSet"))
						{
							return;
						}
						this.InitGridView(0);
						this.ShowPipeInfo(tnSelected.Tag as PipeSet);
						return;
					}
					else
					{
						if (!(name == "BuildingSystem"))
						{
							return;
						}
						this.InitGridView(6);
						BuildingSystem buildingSystem = tnSelected.Tag as BuildingSystem;
						object[] values = new object[]
						{
							buildingSystem.RisePipes.Count,
							buildingSystem.SupplyPipes.At(1).Flow.ToString("F3"),
							buildingSystem.Resistance.ToString("F3")
						};
						this.gvDataShow.Rows.Add(values);
						return;
					}
				}
				else if (num != 601366229u)
				{
					if (num != 1581676155u)
					{
						return;
					}
					if (!(name == "IndoorSystem"))
					{
						return;
					}
					this.InitGridView(3);
					IndoorSystem indoorSystem = tnSelected.Tag as IndoorSystem;
					object[] values2 = new object[]
					{
						indoorSystem.Loops.Count,
						indoorSystem.RateUnbalance.ToString("P2"),
						indoorSystem.AvailPress.ToString("F3"),
						indoorSystem.Resistance.ToString("F3")
					};
					this.gvDataShow.Rows.Add(values2);
					return;
				}
				else
				{
					if (!(name == "RisePipeSystem"))
					{
						return;
					}
					this.InitGridView(5);
					RisePipeSystem risePipeSystem = tnSelected.Tag as RisePipeSystem;
					object[] values3 = new object[]
					{
						risePipeSystem.FloorSystems.Count,
						risePipeSystem.RateUnbalance.ToString("P2")
					};
					this.gvDataShow.Rows.Add(values3);
					return;
				}
			}
			else if (num <= 1931608372u)
			{
				if (num != 1896843152u)
				{
					if (num != 1931608372u)
					{
						return;
					}
					if (!(name == "FloorSystem"))
					{
						return;
					}
					this.InitGridView(4);
					FloorSystem floorSystem = tnSelected.Tag as FloorSystem;
					object[] values4 = new object[]
					{
						floorSystem.InDoorSystem.Count,
						floorSystem.RateUnbalance.ToString("P2")
					};
					this.gvDataShow.Rows.Add(values4);
					return;
				}
				else
				{
					if (!(name == "IndoorLoop"))
					{
						return;
					}
					this.InitGridView(2);
					IndoorLoop indoorLoop = tnSelected.Tag as IndoorLoop;
					object[] values5 = new object[]
					{
						indoorLoop.EquipmentLoops.Count,
						indoorLoop.RateUnbalance.ToString("P2")
					};
					this.gvDataShow.Rows.Add(values5);
					return;
				}
			}
			else if (num != 3476548291u)
			{
				if (num != 4110473035u)
				{
					return;
				}
				if (!(name == "EquipmentLoopSet"))
				{
					return;
				}
				this.InitGridView(1);
				this.ShowEquipmentInfos(tnSelected.Tag as EquipmentLoopSet);
				return;
			}
			else
			{
				if (!(name == "HyCalcPipe"))
				{
					return;
				}
				this.InitGridView(0);
				this.ShowPipeInfo(tnSelected.Tag as HyCalcPipe);
				return;
			}
		}

		private void ShowEquipmentInfos(EquipmentLoopSet set)
		{
			for (int i = 0; i < set.Count; i++)
			{
				object[] values = new object[]
				{
					set[i].Index,
					set[i].EquipmentName,
					set[i].ResistTcValve.ToString("F3"),
					set[i].RateUnbalance.ToString("P2")
				};
				this.gvDataShow.Rows.Add(values);
			}
		}

		private void ShowPipeInfo(PipeSet pSet)
		{
			pSet.ForEach(delegate(HyCalcPipe p)
			{
				this.ShowPipeInfo(p);
			});
		}

		private void ShowPipeInfo(HyCalcPipe pipe)
		{
			object[] values = new object[]
			{
				pipe.Name,
				pipe.Flow.ToString("F3"),
				pipe.DN.ToString("G"),
				pipe.Length.ToString("F3"),
				pipe.ResCoefLocal.ToString("F2"),
				pipe.Velocity.ToString("F3"),
				pipe.Rm.ToString("F3"),
				pipe.Resistancey.ToString("F3"),
				pipe.Resistancej.ToString("F3"),
				pipe.Resistance.ToString("F3")
			};
			this.gvDataShow.Rows.Add(values);
			this.gvDataShow.Rows[this.gvDataShow.RowCount - 1].Tag = pipe;
		}

		private void TSMIFilePath_Click(object sender, EventArgs e)
		{
			SetDataFilePathForm setDataFilePathForm = new SetDataFilePathForm(this.heatingOption.DataFilePath);
			setDataFilePathForm.ShowDialog();
			this.heatingOption.DataFilePath = setDataFilePathForm.FilePath;
			this.heatingOption.Save(Product.UserConfigFile);
		}

		private void TSMIDrawTheory_Click(object sender, EventArgs e)
		{
            if ((int)this.doc.ActiveView.ViewType == 4)
			{
				this.ShowStatusMessage("绘制系统图功能只能在平面视图下使用！", 0);
				return;
			}
			base.DialogResult = DialogResult.Abort;
		}

		private void MainForm_ResizeBegin(object sender, EventArgs e)
		{
			this.cmbPipeDN.Visible = false;
		}

		private UIDocument uiDoc;

		private Document doc;

		private HYHeatingSystemOption heatingOption;

		private BuildingSystem buildingSystem;

		private HeatingSystemCalc heatingCalc;

		private PickUpSystem pick;

		private bool hasSystem;

		private bool bCalced;

		private readonly Image _imgPass = Resources.successface16;

		private readonly Image _imgFaile = Resources.failedface16;

		private readonly Image _imgWait = Resources.waitingface16;

        private System.Windows.Forms.ComboBox cmbPipeDN = new System.Windows.Forms.ComboBox();
	}
}
